package cn.hxlc.tie.demo.modular.zr.equipmentzr.controller;/*
 @author rui
 @date 2021/6/28 --18:31
*/

import cn.hxlc.tie.demo.core.enums.StatusEnum;

import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.web.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.service.EquipmentService;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.model.param.EquipmentParam;

import java.util.List;
import java.util.Map;

import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_SUCCESS_CODE;

@RestController
@ApiResource(name="设备管理",path="/equip")
public class ZrEquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @GetResource(name = "分页查询", path = "/queryListPage", requiredPermission=false, requiredLogin = false)
    public ResponseData queryListPage(EquipmentParam equipmentParam) {

        return ResponseData.success(equipmentService.queryListPage(equipmentParam));
    }

    @PostResource(name="添加",path = "/addOne")
    public ResponseData addOne(@RequestBody EquipmentParam equipmentParam){
        equipmentService.addOne(equipmentParam);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"添加成功","null");
    }

    @GetResource(name="查询详情",path="/detailOne",requiredLogin = false,requiredPermission = false)
    @ApiOperation(value = "查询详情")
    public ResponseData detailOne(@RequestParam Long fid){

        return ResponseData.success( equipmentService.detail(fid));
    }

    @GetResource(name="跟新时查详情",path = "/detailOneList",requiredPermission = false,requiredLogin = false)
    @ApiOperation(value ="编辑时查询详情")
    public ResponseData detailOneList(@RequestParam Long fid){
        return ResponseData.success(equipmentService.detailList(fid));

    }

    @PostResource(name="删除",path="/deleteOne")
    @ApiOperation(value="删除")
    public ResponseData deleteTree(@RequestBody EquipmentParam equipmentParam){
        equipmentService.delete(equipmentParam);
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE,"删除成功",null);
    }

    @PostResource(name = "批量操作", path = "/batchEdit")
    @ApiOperation("批量禁用启用删除")
    public ResponseData batchEdit(@RequestBody Map params) {
        JSONArray jfids = (JSONArray)params.get("fid");
        Object []fid =jfids.toArray();
        Integer fstatus = (Integer)params.get("fstatus");
        equipmentService.batchChangeState(fid, StatusEnum.toEnum(fstatus));
        return ResponseData.success(ResponseData.DEFAULT_ERROR_CODE,"操作成功",null);
    }

    @PostResource(name = "更新操作",path="/update",requiredLogin = false)
    @ApiOperation("更新操作")
    public ResponseData updateOne(@RequestBody EquipmentParam equipmentParam){
        equipmentService.updateOne(equipmentParam);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }



}
